
//String 专门用来表示字符串类型

import java.util.Locale;

class Person {

}

public class Test {

    public static void main12(String[] args) {
        String str1 = "asdf";
        //双引号 引起来的值 就存在字符串常量池当中，如果有，就不存储，直接返回字符串常量池的对象即可
        String str2 = new String("asdf");
        System.out.println(str1 == str2);//false
    }

    //替换两边的空格
    public static void main11(String[] args) {
        String str = "    asdf gg ";
        System.out.println(str);
        String ret = str.trim();
        System.out.println(ret);
    }

    //字符串的截取
    public static void main10(String[] args) {
        String str = "asdfghjkl";
        //返回 从2位置开始截取的所有字符
        String s = str.substring(2);
        System.out.println(s);

        String s2 = str.substring(2,6);//[2,6)  返回新的字符串对象
        System.out.println(s2);
    }

    //字符串的分割
    public static void main9(String[] args) {
        String str1 = "hello good morning";
        String[] strs1 = str1.split(" ");
        for (int i = 0; i < strs1.length; i++) {
            System.out.println(strs1[i]);
        }

        String str2 = "192.168.1.2";
        String[] strs2 = str2.split("\\.");//通过\\转义
        for (int i = 0; i < strs2.length; i++) {
            System.out.println(strs2[i]);
        }

        String str3 = "192\\168\\1\\2";
        String[] strs3 = str3.split("\\\\");//通过\\转义
        for (int i = 0; i < strs3.length; i++) {
            System.out.println(strs3[i]);
        }

        String str4 = "192&168=1";
        String[] strs4 = str4.split("&|=");//通过\\转义
        for (int i = 0; i < strs4.length; i++) {
            System.out.println(strs4[i]);
        }
    }

    //字符串的替换
    public static void main8(String[] args) {
        String str1 = "ababcabcdabcde";
        String ret1 = str1.replace('a','o');
        System.out.println(ret1);

        String ret2 = str1.replace("ab","ooo");
        System.out.println(ret2);

        String ret3 = str1.replaceAll("ab","111");
        System.out.println(ret3);

        //替换第一个ab
        String ret4 = str1.replaceFirst("ab","222");
        System.out.println(ret4);
    }


    //字符串转数组
    public static void main(String[] args) {
        String str1 = "asdfgh";
        char[] ch = str1.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            System.out.print(ch[i]);
        }
        System.out.println();
        System.out.println("========");
        System.out.println(str1);
    }

    //大小写的转换
    public static void main6(String[] args) {
        String str1 = "asdf";
        String str2 = "ZXCV";
        //小写转大写
        System.out.println(str1.toUpperCase());
        //大写转小写
        System.out.println(str2.toLowerCase());
    }

    //数值和字符串的转换
    public static void main5(String[] args) {
        String str1 = String.valueOf(123);
        System.out.println(str1);
        String str2 = String.valueOf(new Person());
        System.out.println(str2);
        System.out.println("====================");

        String str3 = "1234";
        //基本类型对应的 类 类型  所以 基本类型 也提供了面向对象的思想
        int data = Integer.parseInt(str3);
        double data2 = Double.parseDouble(str3);
        System.out.println(data2);


    }

    //字符串的查找
    public static void main4(String[] args) {
        String str1 = "hello";
        char ch = str1.charAt(1);//查找下标为1的字符
        System.out.println(ch);
        //遍历字符串
        for (int i = 0; i < str1.length(); i++) {
            System.out.println(str1.charAt(i));
        }

        String str2 = "ababcabcdabcde";
        //从第一个位置开始找c
        System.out.println(str2.indexOf('c'));
        //从第6个位置开始找c
        System.out.println(str2.indexOf('c',6));
        //在主串当中查找子串 KMP算法
        System.out.println(str2.indexOf("abcd"));
        //从后往前查找
        System.out.println(str2.lastIndexOf('a',8));

    }

    //字符串大小的比较
    public static void main3(String[] args) {
        String str1 = new String("asdf");
        String str2 = new String("aadf");
        //str1 和 str2 进行比较
        System.out.println(str1.compareTo(str2));

        //忽略大小写比较
        String str3 = new String("Zxcv");
        String str4 = new String("zxcv");
        System.out.println(str3.compareToIgnoreCase(str4));


    }



    //字符串的比较是否相同
    public static void main2(String[] args) {
        String str1 = new String("asdf");
        String str2 = new String("asdf");
        System.out.println(str1 == str2);//false
        //比较两个对象所指向的对象当中的内容是否一致
        System.out.println(str1.equals(str2));//true
    }

    public static void main1(String[] args) {
        String str = "hello";
        String str2 = new String("hello");
        System.out.println(str.length());
        System.out.println(str2);

        int[] array = {1,2,3,4};
        System.out.println(array.length);

        String str3 = "";
        System.out.println(str3.length());
        System.out.println(str3.isEmpty());//表示这个引用指向的对象是空的
        String str4 = null;//代表引用不指向任何对象

        //字符数组
        char[] values = {'a','b','c','d'};
        String str5 = new String(values);
        System.out.println(str5);
    }
}
